/**
 * Contains methods used to add and display workouts, including the workout object which represents
 * one specific day's worth of exercise that a user performed.
 * 
 * @author Chris
 */

package b1w1nn1ng.crossf1t.business;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import b1w1nn1ng.crossf1t.db.Database;

/* how to use the calendar class
	datestuff(){ 
	Calendar cal = Calendar.getInstance();
	cal.setTime(new Date());
	System.out.println("Year " + cal.get(Calendar.YEAR));
	cal.set(Calendar.MONTH, 1);
	cal.set(Calendar.DATE, 1);
}*/


public abstract class Workout implements Comparable<Workout>{
	protected int userID;

	protected String type; //TABATA, AMRAP, TIMED

	protected ArrayList<Exercise> exercises;
	protected boolean prescribed;
	protected Calendar cal;
	
	public int getUserID() {
		return userID;
	}

	public void setUserID(int userID) {
		this.userID = userID;
	}
	
	public Calendar getCal() {
		return cal;
	}

	public void setCal(Calendar cal) {
		this.cal = cal;
	}

	public abstract String toHTML();
	
	public abstract  boolean isPr (int id, String type,Exercise[] exercises, int value) throws Exception;
	
	
	public static void incrementWorkoutCount(int userID) throws SQLException, ClassNotFoundException{
		User user = Database.getUserByID(userID);
		user.setNumWorkouts(user.getNumWorkouts()+1);
	}
	
	public static Workout getPR(int id, String type, Exercise exerciseName[]) {
		return Database.getPR(id, type,exerciseName);		//database must implement this method
		//getPR takes an ID and type (AMRAP, Tabata, or Timed) and returns a workout object
		//of that type or null if it doesn't exist	
	}
	
	public ArrayList<Exercise> getExercises() {
		return exercises;
	}

	public void setExercises(ArrayList<Exercise> exercises) {
		this.exercises = exercises;
	}
	
	public void addExercise(Exercise exercise) {
		this.exercises.add(exercise);
	}

	public static Workout getWorkOut(int id, Calendar date) throws SQLException, ClassNotFoundException{
		//talk to Austin about ID (do we need to keep passing it around?)
		return Database.getWorkout(id, date);
		//return null;
	}
	
	public static Workout getTestWORKOUT(){
		//User user = User.getTestUSER();
		int userID = 12345;
//		Exercise exercise;
//		exercise = new Exercise("Push-ups", 7, 10, 0);
		//exercise = null;
		
		
		final int numExercises=12;
		ArrayList<Exercise> ex = new ArrayList<Exercise>();
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		for(int i = 0;i<numExercises;i++)
		{
			ex.add(new Exercise("Push-ups ",1+i,2+i,3+i, c));
		}
		
		Calendar cal = Calendar.getInstance();
		Date d = new Date();
		cal.setTime(d);
		int tabataScore = 5;

//		ex = new Exercise[1];
//		ex[0] = exercise;
		Workout workout1 = new Tabata(userID, ex, true, cal,tabataScore);
		
		return workout1;
	}
	
	public String getType() {
		return type;
	}
	
	public boolean isPrescribed() {
		return prescribed;
	}

	public void setPrescribed(boolean prescribed) {
		this.prescribed = prescribed;
	}

	public static Calendar parseDate(String day){
		Calendar cal = Calendar.getInstance();
		int month = Integer.parseInt(day.substring(0, 2));
		int date = Integer.parseInt(day.substring(3, 5));
		int year = Integer.parseInt(day.substring(6, 10));  
		cal.set(year, month, date);
		return cal;
	}
	
	abstract public void writeToDatabase();
	
}
